#include "mainwindow.h"
#include <iostream>
#include <QString>
#include "ui_mainwindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QFileDialog>
#include <QFile>
#include <QFileInfo>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowTitle("简单记事本");

    //获取菜单栏
    QMenuBar* menuBar = this->menuBar();
    this->setMenuBar(menuBar);

    //添加菜单
    QMenu* menu = new QMenu("菜单");
    menuBar->addMenu(menu);

    //添加菜单项
    QAction* action1 = new QAction("打开");
    QAction* action2 = new QAction("保存");
    QAction* action3 = new QAction("查看文件信息");
    menu->addAction(action1);
    menu->addAction(action2);
    menu->addAction(action3);

    QAction* actionSaveShortcut = new QAction(this);
    actionSaveShortcut->setShortcut(QKeySequence("Ctrl+S"));
    connect(actionSaveShortcut, &QAction::triggered, this, &MainWindow::save);
    this->addAction(actionSaveShortcut);  // 将动作添加到窗口，使其始终有效

    //指定一个输入框
    edit = new QTextEdit();
    QFont font;
    font.setPixelSize(30);
    font.setFamily("微软雅黑");
    edit->setFont(font);
    this->setCentralWidget(edit);

    connect(action1, &QAction::triggered, this, &MainWindow::open);
    connect(action2, &QAction::triggered, this, &MainWindow::save);
    connect(action3, &QAction::triggered, this, &MainWindow::status);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::open()
{
    //1. 先弹出“打开文件”对话框。让用户选择打开哪个文件
    QString path = QFileDialog::getOpenFileName(this);

    //2.文件名显示到状态栏里
    QStatusBar* statusBar = this->statusBar();
    statusBar->showMessage(path, 3000);

    //3.据用户选择的路径，构造一个QFile对象．并打开文件
    QFile file(path);
    bool ret = file.open(QFile::ReadOnly);
    if (ret == false) {
        //打开文件失败
        statusBar->showMessage(path + " 打开文件失败", 3000);
        return;
    }

    //4.读取文件
    QString text = file.readAll();

    //5.关闭文件,千万不要忘记
    file.close();

    //6.读到的内容设置到输入框中
    edit->setText(text);
}

void MainWindow::save()
{
    if (currentFilePath.isEmpty()) {
        //1.先弹出“保存文件”对话框
        currentFilePath = QFileDialog::getSaveFileName(this);
    }

    if (!currentFilePath.isEmpty()) {
        //2.状态栏中显示这个文件名
        QStatusBar* statusBar = this->statusBar();
        statusBar->showMessage(currentFilePath, 3000);

        //3.据用户选择的路径，构造一个QFile对象，并打开文件
        QFile file(currentFilePath);
        bool ret = file.open(QFile::WriteOnly);
        if (ret == false) {
            statusBar->showMessage(currentFilePath + " 打开失败!");
            return;
        }

        //4.写文件
        const QString& text = edit->toPlainText();
        file.write(text.toUtf8());

        //5.关闭文件
        file.close();
    }
}

void MainWindow::status()
{
    //弹出文件对话框，并获取到文件的属性信息
    QString path = QFileDialog::getOpenFileName(this);

    //构造出一个 QFileInfo 对象
    QFileInfo fileInfo(path);

    //打印相关文件信息
    qDebug() << fileInfo.fileName();
    qDebug() << fileInfo.suffix();
    qDebug() << fileInfo.path();
    qDebug() << fileInfo.size();
    qDebug() << fileInfo.isFile();
    qDebug() << fileInfo.isDir();

    std::cout << "wdsa" << std::endl;

}

